package wqh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wqh.domain.AlarmEvent;
import wqh.pojo.MapRet;
import wqh.service.AlarmEventService;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/water/alarm_event")
public class AlarmEventController {

    private static org.slf4j.Logger logger = LoggerFactory.getLogger(AlarmEventController.class);

    @Autowired
    private AlarmEventService alarmEventService;

    @RequestMapping(value = "/getData",method = RequestMethod.GET)
    public Object getData(@RequestParam(value = "draw",required = true) int draw,
                          @RequestParam(value = "start",required = true) int start,
                          @RequestParam(value = "length",required = true) int length
//                          @RequestParam(value = "order[0][column]",required = true) int column,
//                          @RequestParam(value = "order[0][dir]",required = true) String order,
//                          @RequestParam(value = "search[value]",required = true) String search
    )
    {
        try {
            PageHelper.startPage(start / length + 1, length);
            List<AlarmEvent> alarmEventList = alarmEventService.findAllRecord();
            Integer count = alarmEventService.selectCount();
            @SuppressWarnings("unchecked")
            PageInfo page = new PageInfo(alarmEventList);
            return MapRet.DATATABLES(draw,count,new Long(page.getTotal()).intValue(),alarmEventList);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("显示报警事件异常：" + e.getMessage());
            return MapRet.SQL_ERROR(e);
        }
    }

}
